/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/

#ifndef SCREENCASTING_TEACHING_SYSTEM___CLIENT__MEDIA___CAPTURE_WORKSPACE_X11_ALSA_IMPL_H_
#define SCREENCASTING_TEACHING_SYSTEM___CLIENT__MEDIA___CAPTURE_WORKSPACE_X11_ALSA_IMPL_H_

#include "capture_workspace.h"
#include "../../common/comm/thread.h"
#include "../../common/model/lesson.h"
#include "../../common/comm/lock.h"
#include <log4cxx/logger.h>

extern "C" {
	#ifndef INT64_C
	#define INT64_C(c) (c ## LL)
	#define UINT64_C(c) (c ## ULL)
	#endif
	#include <libavcodec/avcodec.h>
	#include <libswscale/swscale.h>
	#include <libavformat/avformat.h>
	#include <libavdevice/avdevice.h>
	#include <libavfilter/avfilter.h>
}

/**
 *
 */
class CaptureWorkspaceX11AlsaImpl : public CaptureWorkspace {
public:
	CaptureWorkspaceX11AlsaImpl();
	virtual ~CaptureWorkspaceX11AlsaImpl();
	virtual void startScreencast(Lesson *lesson);
	virtual void stopScreencast();

private:
	log4cxx::LoggerPtr logger_;
	bool capturing_;
	Lesson *activeLesson_;

	/**
	 *
	 */
	class CaptureVideoX11 : public Thread {
	public:
		CaptureVideoX11(CaptureWorkspaceX11AlsaImpl &outer);
		virtual ~CaptureVideoX11();
		virtual void run();

	private:
		void initVideoCapture();
		void cleanUp();
		void transcode();

		bool initialized;
		CaptureWorkspaceX11AlsaImpl &outer_;

		AVInputFormat   *videoInputFormat_;
		AVFormatContext *videoInputFormatContext_;
		AVCodecContext  *videoInputDecoderContext_;
		AVCodec         *videoInputDecoder_;
		AVCodec		    *videoInputEncoder_;
		AVCodecContext  *videoInputEncoderContext_;
		AVFrame         *pFrame_;
		AVFrame         *pFrameYUV_;
		int             nbytes_;
		uint8_t         *outbuffer_;
		SwsContext      *img_convert_ctx_;
	};

	/**
	 *
	 */
	class CaptureAudioAlsa : public Thread {
	public:
		CaptureAudioAlsa(CaptureWorkspaceX11AlsaImpl &outer);
		virtual ~CaptureAudioAlsa();
		virtual void run();

	private:
		void initAudioCapture();
		void cleanUp();
		void transcode();

		bool initialized_;
		CaptureWorkspaceX11AlsaImpl &outer_;

		AVInputFormat   *audioInputFormat_;
		AVFormatContext *audioInputFormatContext_;
		AVCodecContext  *audioInputDecoderContext_;
		AVCodec         *audioInputDecoder_;
		AVCodec		    *audioInputEncoder_;
		AVCodecContext  *audioInputEncoderContext_;
		int16_t         *samples_;
		uint8_t         *audioOutbuf_;
		int             audioOutbufSize_;
		int             audioInputFrameSize_;
	};

	CaptureVideoX11  videoThread_;
	CaptureAudioAlsa audioThread_;
	Mutex initializeAndCleanUpFFMPEGmutex_;
};

#endif /* SCREENCASTING_TEACHING_SYSTEM___CLIENT__MEDIA___CAPTURE_WORKSPACE_X11_ALSA_IMPL_H_ */

